package br.ucb.webresource;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import br.ucb.bean.Calendario;
import br.ucb.bean.CalendarioShort;
import br.ucb.bean.PlanoDeEnsino;
import br.ucb.bean.Turma;
import br.ucb.bean.TurmaShort;
import br.ucb.controller.PlanoDeEnsinoController;
import br.ucb.controller.TurmaController;

import com.google.gson.Gson;

@Path("/plano")
public class PlanoResource implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@POST
	@Path("/getPlano")
	@Produces("application/json")
	@Consumes("application/json")
	public PlanoDeEnsino getPlanoByTurma(TurmaShort t) {
		Turma turma= new TurmaController().findById(t.getTurmaId());
		return removeCrossReference(new PlanoDeEnsinoController().findPlanoByDisciplinaId(turma.getDisciplina().getCodigo()));
	}
	
	private PlanoDeEnsino removeCrossReference(PlanoDeEnsino plano) {
		plano.setCalendarios(null);
		plano.setDisciplina(null);
		return plano;
	}

	@POST
	@Path("/getCalendarios")
	@Produces("application/json")
	@Consumes("application/json")
	public ArrayList<CalendarioShort> getTurmas(PlanoDeEnsino plano){
		return buildCalendarioShort(new PlanoDeEnsinoController().findById(plano.getCodigo()).getCalendarios());
		
	}
	
	

	private ArrayList<CalendarioShort> buildCalendarioShort(
			Collection<Calendario> calendarios) {
		ArrayList<CalendarioShort> cShort = new ArrayList<CalendarioShort>();
		for (Calendario calendario : calendarios) {
			cShort.add(new CalendarioShort(calendario.getTipoEvento(), new SimpleDateFormat("dd/MM/yyyy").format(calendario.getData())));
		}
		return cShort;
	}

	@POST
	@Path("getCalendariosGSON")
	@Produces("application/json")
	@Consumes("application/json")
	public String getTurmasGSON(PlanoDeEnsino plano){
		return new Gson().toJson(new PlanoDeEnsinoController().findById(plano.getCodigo()).getCalendarios());
	}

}
